package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.FuelInfo;
import com.ruoyi.system.mapper.FuelInfoMapper;
import com.ruoyi.system.service.IFuelInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * 参数管理模块
 * @author ytl
 */
@Service
public class FuelInfoServiceImpl implements IFuelInfoService {

    @Autowired
    public FuelInfoMapper fuelInfoMapper;

    private static final Logger log = LoggerFactory.getLogger(FuelInfoServiceImpl.class);

    @Override
    @Transactional
    public int insertFuelInfo(FuelInfo fuelInfo) {
        int rows = fuelInfoMapper.insertFuelInfo(fuelInfo);
        return rows;
    }

    @Override
    public List<FuelInfo> listFuelInfo(FuelInfo fuelInfo) {
        return fuelInfoMapper.listFuelInfo(fuelInfo);
    }

    @Override
    public List<FuelInfo> listFuelInfoAll() {
        return fuelInfoMapper.listFuelInfoAll();
    }

    @Override
    @Transactional
    public int deleteFuelInfo(FuelInfo fuelInfo) {
        return fuelInfoMapper.deleteFuelInfo(fuelInfo);
    }

    @Override
    @Transactional
    public int updeteFuelInfo(FuelInfo fuelInfo) {
        return fuelInfoMapper.updeteFuelInfo(fuelInfo);
    }

    @Override
    public List<FuelInfo> listFuelTenantInfo() {
        return fuelInfoMapper.listFuelTenantInfo();
    }

    @Override
    public String importFuelInfo(List<FuelInfo> fuelInfoList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(fuelInfoList) || fuelInfoList.size() == 0)
        {
            throw new BusinessException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (FuelInfo fuelInfo:fuelInfoList){
            try {
                    List<FuelInfo> list = fuelInfoMapper.listFuelInfo(fuelInfo);
                    if(list.size()==0){
                        fuelInfo.setFuelUUID(UUID.randomUUID().toString());
                        fuelInfo.setCreateBy(operName);
                        this.insertFuelInfo(fuelInfo);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、楼层 " + fuelInfo.getFloorName() +"、租户名称"+fuelInfo.getTenantName()
                                +"、租户面积"+fuelInfo.getTenantArea()+"、空调油费"+fuelInfo.getFuel_amt()+"、空调油费合计"+fuelInfo.getFuel_total_amt()+ " 导入成功");
                    } else if (isUpdateSupport)
                    {
                        fuelInfo.setFuelUUID(list.get(0).getFuelUUID());
                        fuelInfo.setUpdateBy(operName);
                        this.updeteFuelInfo(fuelInfo);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、楼层 " + fuelInfo.getFloorName() +"、租户名称"+fuelInfo.getTenantName()
                                +"、租户面积"+fuelInfo.getTenantArea()+"、空调油费"+fuelInfo.getFuel_amt()+"、空调油费合计"+fuelInfo.getFuel_total_amt()+ " 更新成功");
                    }
                    else {
                        failureNum++;
                        failureMsg.append("<br/>" + successNum + "、楼层 " + fuelInfo.getFloorName() +"、租户名称"+fuelInfo.getTenantName()
                                +"、租户面积"+fuelInfo.getTenantArea()+"、空调油费"+fuelInfo.getFuel_amt()+"、空调油费合计"+fuelInfo.getFuel_total_amt()+ " 已存在");
                    }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + successNum + "、楼层 " + fuelInfo.getFloorName() +"、租户名称"+fuelInfo.getTenantName()
                    +"、租户面积"+fuelInfo.getTenantArea()+"、空调油费"+fuelInfo.getFuel_amt()+"、空调油费合计"+fuelInfo.getFuel_total_amt()+ " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        return successMsg.toString();
    }

    @Override
    public FuelInfo listAllFuelInfo(FuelInfo fuelInfo) {
        return fuelInfoMapper.listAllFuelInfo(fuelInfo);
    }
}
